package net.mysoft.budgetsoft.system.action;

import net.mysoft.budgetsoft.system.domain.PageData;
import net.mysoft.budgetsoft.system.domain.TRole;
import net.mysoft.budgetsoft.system.domain.TUser;
import net.mysoft.budgetsoft.system.service.RoleService;
import net.mysoft.framework.base.BaseAction;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Namespace("/role")
@Scope("prototype")
@Controller
public class RoleAction extends BaseAction {
	
	private TRole role;
	
	@Autowired
	private RoleService roleService;

	/**
	 * 
	 */
	private static final long serialVersionUID = -5412436573315759621L;
	
	/**
	 * 角色列表
	 * @return
	 */
	@Action("datagrid")
	public String datagrid() {
		super.writeJson(roleService.getRoleDataGrid(super.getParaMap()));
		return null;
	}
	
	/**
	 * 新增角色
	 * @return
	 */
	@Action("add")
	public String add() {
		try {
			roleService.addRole(role);
			success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			fail("保存失败！！");
		}
		return null;
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	@Action("del")
	public String del() {
		try {
			roleService.delRoles(super.getPara("ids"));
			success("删除成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			fail("删除失败！！");
		}
		return null;
	}
	
	/**
	 * 菜单树
	 * @return
	 */
	@Action("menu")
	public String menu() {
		writeJson(roleService.getMenuTree(role.getId()));
		return null;
	}
	
	/**
	 * 菜单保存
	 * @return
	 */
	@Action("menu_save")
	public String saveMenu() {
		try {
			roleService.saveRoleMenu(getPara("roleId"), getPara("menuIds"));
			success("授权成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			fail("授权失败！！");
		}
		return null;
	}
	
	@Action("org")
	public String org() {
		super.writeJson(roleService.getOrgTree());
		return null;
	}
	
	@Action("user_datagrid")
	public String userDatagrid() {
		String orgId = getPara("orgId");
		if(StringUtils.isBlank(orgId)) super.writeJson(new PageData<TUser>());
		else super.writeJson(roleService.getUserDataGrid(orgId));
		return null;
	}
	
	@Action("user_save")
	public String saveUser() {
		try {
			roleService.saveRoleUser(getPara("roleId"), getPara("userIds"));
			success("分配成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			fail("分配失败！！");
		}
		return null;
	}

	public TRole getRole() {
		return role;
	}

	public void setRole(TRole role) {
		this.role = role;
	}
	
}
